package com.hfkh;

import java.util.Scanner;

/**
 * 汽车公司的类
 */
public class CarManager {
    /**
     * 文本扫描器
     */
    static Scanner input = new Scanner(System.in);
    static String no;
    static boolean sta = false;
    static int cou = 0;
    static int day = 0;
    static int dre;

    //车辆集合，汽车对象数组
    static Car[] cars = new Car[]{
          new Car("皖A-88888",false,0,0,500),
          new Car("皖A-77777",true,12,2,600),
          new Car("皖A-66666",false,10,0,700),
          new Car("皖A-55555",false,10,0,800),
          new Car("皖A-22222",true,10,7,300),
          null
    };
    /**
     * 新增入库
     * @param car 汽车对象
     * @return
     */
    public static boolean addCar(Car car) {
        if(cars.length<6){
            for (int i = 0; i <cars.length ; i++) {
                if (cars[i] == null){
                    System.out.println("请输入车辆牌号：");
                    no = input.next();
                    System.out.println("请输入日租金：");
                    dre = input.nextInt();
                    cars[i]=new Car(no,sta,cou,day,dre);
                    System.out.println("添加成功！");
                }
            }
            return true;
        }else {
            System.out.println("车库已满，无法添加");
        }
        return false;
    }
    /**
     * 查看车辆信息
     */
    public static void viewCars(){
        String str ="序号"+"\t\t\t\t"+ "车辆信息"+"\t\t\t\t"+"状态"+"\t\t\t"+ "借出日期"+"\t\t\t"+ "借出次数"+"\t\t\t"+ "日租金"+"\n";
        for (int i = 0; i <cars.length ; i++) {
            if (cars[i] != null){
                str = str+i+"\t\t\t\t"+cars[i].licenseNo+"\t\t\t"+cars[i].status+"\t\t\t"
                        +cars[i].count+"\t\t\t\t"+cars[i].days+"\t\t\t"+cars[i].dailyRent+"\n";
            }
        }
        System.out.println(str);
    }

    /**
     * 通过车牌号删除车辆信息
     * @param licenseNo
     * @return
     */
    public static boolean deleteCar(String licenseNo) {
        System.out.println("请输入车牌号：");
        String no = input.next();
        for (int i = 0; i <cars.length ; i++) {
            if (cars[i].licenseNo == no){
                for (int j = i; j <cars.length - 1 ; j++) {
                    cars[j] = cars[j+1];
                }
                cars[cars.length-1] = null;
                System.out.println("淘汰成功！");
                break;
            }else {
                System.out.println("该车不存在！");
                return false;
            }
        }
        return true;
    }

    /**
     * 借出车辆
     * @param licenseNo
     * @return
     */
    public static boolean lendCar(String licenseNo) {
        System.out.println("请输入车牌号：");
        String name = input.next();
        for (int i = 0; i <cars.length ; i++) {
            if (name.equals(cars[i].licenseNo)){
                if (cars[i].status == false) {
                    System.out.println("借车成功！");
                    cars[i].status = true;
                    return true;
                }
            }
        }
        System.out.println("该车不存在或已出库！");
        return false;
    }

    /**
     * 归还车辆
     * @param licenseNo
     * @return
     */
    public static boolean giveBackCar(String licenseNo) {
        System.out.println("请输入车牌号：");
        String no = input.next();
        for (int i = 0; i <cars.length ; i++) {
            if (cars[i].licenseNo.equals(no) && cars[i].status == true){
                System.out.println("还车成功！");
                cars[i].status = false;
                return true;
            }
        }
        System.out.println("该车不存在或已在库！");
        return false;
    }
//    public static boolean giveBackCar(String licenseNo) {
//        System.out.println("请输入车牌号：");
//        String no = input.next();
//        for (int i = 0; i <cars.length ; i++) {
//            if (cars[i].licenseNo.equals(no) && cars[i].status == true){
//                System.out.println("还车成功！");
//                cars[i].status = false;
//                break;
//            }else {
//                System.out.println("该车不存在或已在库！");
//                return false;
//            }
//        }
//        return true;
//    }
    /**
     * 菜单切换
     */
    public static void showMenu() {
        int num = 0;
        boolean isExit = false;
        do{
            System.out.println("欢迎进入良友汽车租赁公司");
            System.out.println("\t1.新增车辆");
            System.out.println("\t2.查看车辆");
            System.out.println("\t3.淘汰车辆");
            System.out.println("\t4.租赁车辆");
            System.out.println("\t5.归还车辆");
            System.out.println("\t6.退出系统");
            int menuNum = input.nextInt();
            switch (menuNum){
                case 1:
                    System.out.println("当前位置：良友汽车》》新增车辆");
                    addCar(null);
                    break;
                case 2:
                    System.out.println("当前位置：良友汽车》》查看车辆");
                    viewCars();
                    break;
                case 3:
                    System.out.println("当前位置：良友汽车》》淘汰车辆");
                    deleteCar(null);
                    break;
                case 4:
                    System.out.println("当前位置：良友汽车》》租赁车辆");
                    lendCar(null);
                    break;
                case 5:
                    System.out.println("当前位置：良友汽车》》归还车辆");
                    giveBackCar(null);
                    break;
                case 6:
                    isExit = true;
                    break;
                default:
                    System.out.println("输入错误！");
            }
            if(isExit){
                break;
            }else {
                System.out.println("输入0返回菜单，请输入：");
                num = input.nextInt();
            }
        }while (num==0);
        System.out.println("感谢使用本系统，欢迎下次使用");
    }

    public static void main(String[]args){
        showMenu();
        }
}
